angular.module('controllers', ["ionic"])
.controller("loginCtrl",function($scope,dataService){
	$scope.login=function(){
		var data = dataService.login();
	}
	
	$scope.getList=function(){
		var promise = dataService.getNewsList();
		 promise.then(function(data) {  // 调用承诺API获取数据 .resolve  
		    }, function(data) {  // 处理错误 .reject  
		        $scope.user = {error: '用户不存在！'};  
		    }); 
		
	}
	
	
})
.controller('newsDetailCtrl', function ($scope,$timeout,dataService) {
	$scope.init=function(){
		var items = dataService.dataList;
		var currItem = dataService.currItem;
		$scope.title=currItem.title;
		$scope.body=currItem.body;
	}
	
})
.controller('newsCtrl', function ($scope,$timeout,dataService,$state) {
	
	$scope.init=function(){
		var promise=  dataService.getNewsList();
		 promise.then(function(data) {  // 调用承诺API获取数据 .resolve  
			 $scope.items = data.newslist;
			 dataService.dataList = data.newslist;
		    }, function(data) {  // 处理错误 .reject  
		        $scope.user = {error: '新闻列表不存在！'};  
		    }); 
	}
	
	$scope.showDetail=function(id){
		var promise=  dataService.getNewsDetail(id);
		 promise.then(function(data) {
			 dataService.currItem = data;
			 $state.go('newsDetail');
			 
		 }, function(data) {  // 处理错误 .reject  
		        $scope.user = {error: '新闻不存在！'};  
		    });
	}
    	 
    
    $scope.doRefresh = function() {
        
        console.log('Refreshing!');
        $timeout( function() {

        $scope.items.push('New Item ' + Math.floor(Math.random() * 1000) + 4);
        $scope.items.push('New Item ' + Math.floor(Math.random() * 1000) + 4);
        $scope.items.push('New Item ' + Math.floor(Math.random() * 1000) + 4);
        $scope.items.push('New Item ' + Math.floor(Math.random() * 1000) + 4);
        //Stop the ion-refresher from spinning
        $scope.$broadcast('scroll.refreshComplete');
        
        }, 1000);
        var d =  dataService.getNewsList();
		alert(JSON.stringify(345));
    };
})
;